package com.xhwl.data.config;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.enums.AppTypeEnum;
import com.xhwl.common.exception.DataException;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.pojo.cent.sdata.App;
import com.xhwl.common.pojo.cent.sdata.devplatform.Developer;
import com.xhwl.common.utils.StringUtils;
import com.xhwl.data.service.IAppService;
import com.xhwl.data.service.devplatform.IDeveloperService;
import com.xhwl.data.util.RedisUtils;
import com.xhwl.data.util.SpringContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebArgumentResolver;
import org.springframework.web.context.request.NativeWebRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;

/**
 * @Author: luyi
 * @Description:
 * @Date: Create in 下午5:21 2022/7/20
 */
@Component
public class WebDeveloperArgResolver implements WebArgumentResolver {

    private static final Logger log = LoggerFactory.getLogger(WebDeveloperArgResolver.class);

    private final String ACCOUNT_SAVE_PREFIX = "USERINFO_";

    private final String ADMIN_ACCOUNT_SAVE_PREFIX = "ADMIN_USERINFO_";

    @Override
    public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest){
        if (null != methodParameter.getParameterType() && methodParameter.getParameterType().equals(Developer.class)) {
            HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
            //来自网关解析token后，往请求头新增的userId
            String userId = request.getHeader("userId");
            if (StringUtils.isEmpty(userId)){
                return null;
            }
            String appId = request.getHeader("appId");
            IAppService appService = SpringContextUtils.getBean(IAppService.class);
            App app = appService.getByAppId(appId);
            if(null == app){
                throw new DataException("app {} 不存在");
            }
            String key;
            if(AppTypeEnum.SYSTEM.type == app.getAppType().intValue()){
                key = appId + "_" + ADMIN_ACCOUNT_SAVE_PREFIX + userId;
            }else{
                key = appId + "_" + ACCOUNT_SAVE_PREFIX + userId;
            }
            RedisUtils redisUtils = SpringContextUtils.getBean(RedisUtils.class);
            Object accountInfo = redisUtils.get(key);
            if (Objects.nonNull(accountInfo)) {
                JSONObject accountInfo1 = (JSONObject) accountInfo;
                Object developId = accountInfo1.get("developId");
                if(null == developId){
                    log.warn("账号 {} 没有开发者信息", userId);
                    return null;
                }
                IDeveloperService bean = SpringContextUtils.getBean(IDeveloperService.class);
                return bean.getById((Integer)developId);
            }
            log.info("不存在当前账号的登录信息，userId={}", userId);
//            IDeveloperService bean = SpringContextUtils.getBean(IDeveloperService.class);
//            return bean.getById(app.getDevelopId());
        }
        return UNRESOLVED;
    }

}
